package com.edgar.fileupload;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.android.volley.toolbox.ImageLoader;
import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestManager;
import com.edgar.baselibrary.view.BaseNetworkImageView;

import java.util.List;

/**
 * Created by edgar on 2016/2/6.
 */
public class LoadImageActivity extends AppCompatActivity{
    private static final String IMAGE_URL = "http://b.hiphotos.baidu.com/image/pic/item/9825bc315c6034a857770337ce134954082376ea.jpg";
    private static final String[] URLS = new String[]{
            "http://b.hiphotos.baidu.com/image/pic/item/9825bc315c6034a857770337ce134954082376ea.jpg",
            "http://h.hiphotos.baidu.com/image/pic/item/8718367adab44aedb108e731b71c8701a08bfba5.jpg",
            "http://pic8.nipic.com/20100701/5290458_114840036316_2.jpg",
            "http://pic37.nipic.com/20140209/8821914_163234218136_2.jpg",
            "http://img1.imgtn.bdimg.com/it/u=2048246522,1244394203&fm=206&gp=0.jpg",
            "http://g.hiphotos.baidu.com/image/h%3D360/sign=e763ca26b11bb0519024b52e067ada77/0df3d7ca7bcb0a46bc0b4d3d6963f6246b60af8d.jpg",
            "http://pic30.nipic.com/20130604/11753396_230726098000_2.jpg",
            "http://img0.imgtn.bdimg.com/it/u=2905767175,3720031287&fm=21&gp=0.jpg",
            "http://b.hiphotos.baidu.com/image/h%3D360/sign=a813da3172094b36c4921deb93ce7c00/810a19d8bc3eb135aa449355a21ea8d3fc1f4458.jpg",
            "http://e.hiphotos.baidu.com/image/h%3D360/sign=b1e0f4ea83cb39dbdec06150e01609a7/2f738bd4b31c8701cdaf22e6257f9e2f0708ff91.jpg",
            "http://g.hiphotos.baidu.com/image/h%3D360/sign=17f52f23612762d09f3ea2b990ec0849/5243fbf2b21193130e676fb067380cd791238d8e.jpg",
            "http://b.hiphotos.baidu.com/image/h%3D360/sign=0c8b434a00087bf462ec51efc2d3575e/37d3d539b6003af32b99a061372ac65c1038b68b.jpg",
            "http://img5.imgtn.bdimg.com/it/u=2381533879,680891752&fm=206&gp=0.jpg",
            "http://img2.imgtn.bdimg.com/it/u=4070682482,1348263832&fm=206&gp=0.jpg",
            "http://img5.imgtn.bdimg.com/it/u=2381533879,680891752&fm=206&gp=0.jpg",
            "http://img0.imgtn.bdimg.com/it/u=793636042,551027666&fm=206&gp=0.jpg",
            "http://img1.imgtn.bdimg.com/it/u=202092,824708011&fm=206&gp=0.jpg",
            "http://img1.imgtn.bdimg.com/it/u=3291139481,4041685872&fm=206&gp=0.jpg",
            "http://img0.imgtn.bdimg.com/it/u=41705772,971220606&fm=206&gp=0.jpg",
            "http://img5.imgtn.bdimg.com/it/u=702660416,2522456247&fm=206&gp=0.jpg",
            "http://img2.imgtn.bdimg.com/it/u=1388498650,2529921706&fm=206&gp=0.jpg",
            "http://img3.imgtn.bdimg.com/it/u=974690700,3125013225&fm=206&gp=0.jpg",
            "http://b.hiphotos.baidu.com/image/pic/item/9825bc315c6034a857770337ce134954082376ea.jpg",
            "http://h.hiphotos.baidu.com/image/pic/item/8718367adab44aedb108e731b71c8701a08bfba5.jpg",
            "http://pic8.nipic.com/20100701/5290458_114840036316_2.jpg",
            "http://pic37.nipic.com/20140209/8821914_163234218136_2.jpg",
            "http://img0.imgtn.bdimg.com/it/u=929213041,2471617505&fm=206&gp=0.jpg",
            "http://img2.3lian.com/2014/f4/102/d/90.jpg",
            "http://pic31.nipic.com/20130723/12122227_161419837000_2.jpg",
            "http://news.jsyks.com/photo/img1c.xgo-img.com.cn/pics/1486/1485893.jpg",
            "http://images.ali213.net/picfile/pic/2014/04/29/927_20140429152033519.jpg",
            "http://pic1cn.xingyun.cn/media/users/post/023/16/100200901994_231621.jpg",
            "http://image.tianjimedia.com/uploadImages/2013/324/5QS2D0CQQ4EL.jpg",
            "http://img4.imgtn.bdimg.com/it/u=8724144,3470286526&fm=206&gp=0.jpg",
            "http://f2.dn.anqu.com/down/YjIwOQ==/allimg/1208/48-120PGA511.jpg",
            "http://jd.shijue.cvimage.cn/asset/50a5041fe744f91321000001.jpg",
            "http://www.33.la/uploads/20140525bztp/8623.jpg",
            "http://img.pconline.com.cn/images/upload/upc/tx/itbbs/1205/30/c4/11813491_1338367992109_2_1024x1024it.JPG",
            "http://d.3987.com/qingchun_141225/006.jpg",
            "http://img0.imgtn.bdimg.com/it/u=2832465197,2803256291&fm=206&gp=0.jpg",
            "http://s16.sinaimg.cn/mw690/c427726dt7cdfe40e9abf&690"
    };

    private int mImageThumbSize;
    private int mImageThumbSpacing;

    private GridView mGridView;
    private ImageListAdapter mAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mImageThumbSize = getResources().getDimensionPixelSize(R.dimen.image_thumbnail_size);
        mImageThumbSpacing = getResources().getDimensionPixelSize(R.dimen.image_thumbnail_spacing);
        setContentView(R.layout.load_img_layout);
        mAdapter = new ImageListAdapter(this);
        mGridView = (GridView) findViewById(R.id.gridView);
        mAdapter = new ImageListAdapter(getApplicationContext());
        mGridView.setAdapter(mAdapter);
        mGridView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                if (mAdapter.getNumColumns() == 0) {
                    final int numColumns = (int) Math.floor(
                            mGridView.getWidth() / (mImageThumbSize + mImageThumbSpacing));
                    if (numColumns > 0) {
                        final int columnWidth =
                                (mGridView.getWidth() / numColumns) - mImageThumbSpacing;
                        mAdapter.setItemHeight(columnWidth);
                        mGridView.getViewTreeObserver()
                                .removeGlobalOnLayoutListener(this);
                    }
                }
            }
        });
//        BaseNetworkImageView baseNetworkImageView = (BaseNetworkImageView) findViewById(R.id.volley_img);
//        baseNetworkImageView.setImageUrl(IMAGE_URL, ((IApp) getApplicationContext()).getImageLoader());
//        Glide.with(this).load(IMAGE_URL).into((ImageView) findViewById(R.id.img));
    }

    private static class ImageListAdapter extends BaseAdapter{

        private LinearLayout.LayoutParams layoutParams;
        private LayoutInflater inflater;
        private ImageLoader imageLoader;
        private RequestManager glideManager;
        private int mNumColumns;
        public ImageListAdapter(Context context){
            glideManager = Glide.with(context);
            this.inflater = LayoutInflater.from(context);
            IApp app = (IApp) context.getApplicationContext();
            imageLoader = app.getImageLoader();
        }

        @Override
        public int getCount() {
            return URLS.length;
        }

        @Override
        public Object getItem(int position) {
            return URLS[position];
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder;
            if(convertView == null){
                convertView = inflater.inflate(R.layout.img_list_item,null);
                viewHolder = new ViewHolder();
                viewHolder.imageView = (BaseNetworkImageView) convertView.findViewById(R.id.volley_img);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            if(layoutParams != null){
                viewHolder.imageView.setLayoutParams(layoutParams);
            }
//            glideManager.load((String)getItem(position)).into(viewHolder.imageView);
            viewHolder.imageView.setDefaultImageResId(R.drawable.empty_photo);
            viewHolder.imageView.setErrorImageResId(R.drawable.empty_photo);
//            viewHolder.imageView.setImageResource(R.drawable.empty_photo);
            viewHolder.imageView.setImageUrl((String) getItem(position),imageLoader);
            return convertView;
        }

        public void setItemHeight(int height){
            mNumColumns = height;
            layoutParams =
                    new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,height);
            notifyDataSetChanged();
        }

        public int getNumColumns() {
            return mNumColumns;
        }

        static class ViewHolder{
            BaseNetworkImageView imageView;
        }
    }
}